package com.minelx.woods.task.core;

import com.minelx.statementsplus.cha.base.ChaSession;
import com.minelx.util.multithread.IWorking;

import java.util.function.Predicate;

public class SqlTaskService implements AutoCloseable {

	public static final int LEAF_WORKERS_COUNT = 3;

	private final PostTreeTaskService postTreeTaskService;

	private final Predicate<SqlTaskValues> neededSplitting;

	public SqlTaskService(ChaSession session) {
		this(session, LEAF_WORKERS_COUNT);
	}

	public SqlTaskService(ChaSession session, int leafWorkersCount) {
		neededSplitting = whatIf -> session.explain(whatIf.combinedSql().major().head().content());
		postTreeTaskService = new PostTreeTaskService(session, leafWorkersCount);
	}

	public IWorking submit(ICombinedSqlTask sqlTask) {
		return postTreeTaskService.post(sqlTask.split(neededSplitting));
	}

	public void doTask(ICombinedSqlTask sqlTask) {
		postTreeTaskService.doTask(sqlTask.split(neededSplitting));
	}

	public void close() {
		postTreeTaskService.close();
	}
}
